package 树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Demo11 {
    /*
    * bfs
    * */
    static class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> lists = new ArrayList<>();
            if(root == null){
                return lists;
            }
            Queue<TreeNode> que = new LinkedList<>();
            que.offer(root);
            while(!que.isEmpty()){
                int levelCount = que.size();
                List<Integer> list = new ArrayList<>();
                while(levelCount > 0){
                    TreeNode node = que.poll();
                    if(node.left != null) que.offer(node.left);
                    if(node.right != null) que.offer(node.right);
                    list.add(node.val);
                    levelCount--;
                }
                lists.add(list);
            }
            return lists;
        }
    }
}
